﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace BaseLibrary
{
    static class Postget
    {
        ///Post2Http(url, Encoding.GetEncoding("gb2312"), poststr2
        /// <summary>
        /// 向指定网址发送字符串
        /// </summary>
        /// <param name="url">url地址</param>
        /// <param name="en">字符编码</param>
        /// <param name="poststr">发送字符串</param>
        /// <returns></returns>
        public static string Post2Http(string url, Encoding en, string poststr)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                request.CookieContainer = new CookieContainer();
                request.ContentType = "application/x-www-form-urlencoded";
                request.Method = "post";
                byte[] bytes = en.GetBytes(poststr);
                request.ContentLength = bytes.Length;
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(bytes, 0, bytes.Length);
                requestStream.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), en);
                string str = reader.ReadToEnd();
                reader.Close();
                return str;
            }
            catch
            {
                return "";
            }
        }

    }
}
